class Model {

    constructor(){
        this.init()
    }

    init() {
        this.initUrl();
        this.initUploadModal();
        this.bindEvents();
    }
    initUrl(){
        this.fileContentUrl = "file/content";
        this.fileRenameUrl = "file/rename";
        this.fileCopyUrl = "file/copy";
        this.fileDeleteUrl = "file/delete";
        this.fileDownloadUrl = "files/download";
        this.fileContentEditorUrl = "file/content/editor";
    }

    initUploadModal (){

        $('#fileupload-input').fileupload({
            dataType: 'json',
            url: "files/upload",
            done: function (e, data) {
                $("tr:has(td)").remove();
                $.each(data.result, function (index, file) {
                    $("#upload-files").append(
                        $('<tr/>')
                            .append($('<td/>').text(file.fileName))
                            .append($('<td/>').text(file.fileSize))
                            .append($('<td/>').text(file.fileType))
                            .append($('<td/>').text("上传成功！"))
                    );
                });
            },

            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .bar').css(
                    'width',
                    progress + '%'
                );
            },
            dropZone: $('#dropzone')
        });
    };

    bindEvents(){

        $("#copy-submit").on("click", () => {
            console.log("copy-submit")
            $.getJSON(this.fileCopyUrl, $("#copy-form").serialize(), result => {

                if (result.code == 200) {
                    $("#copy-modal").modal("hide");
                    enterDir(currentDir);
                } else {
                    alert(result.message);
                }
            });
        });
        $("#rename-submit").on("click", () => {
            console.log("rename-submit")
            $.getJSON(this.fileRenameUrl, $("#rename-form").serialize(), result => {

                if (result.code == 200) {
                    $("#rename-modal").modal("hide");
                    enterDir(currentDir);
                } else {
                    alert(result.message);
                }
            });
        });
        console.log("edit-submit",$("#edit-submit"))
        $("#edit-submit").on("click",  () => {
            console.log("edit-submit",$("#edit-submit"))
            var textarea = $("#edit-textarea").val();
            var title = $("#edit-title-id").text();
            var name = Tools.getCurrentName(title);
            $.ajax({
                // cache:true,// 保留缓存数据
                type: "post",
                url: this.fileContentEditorUrl,// 这是我在后台接受数据的文件名
                data: {text: textarea, "name": name},
                error: request => {// 请求失败之后的操作
                    console.log(request);
                },
                success: data => {// 请求成功之后的操作
                    console.log("success");
                    $("#edit-modal").modal("hide");
                }
            });
        });
    }

}



